﻿Imports System.IO
Imports System.Configuration

''' <summary>
''' Log 紀錄
''' </summary>
''' <remarks></remarks>
Public Class clsLog

    Public Enum enumMsgType
        SQLMsg = 1
        ErrorMsg = 2
        General = 3
    End Enum


    Private mDefaultLogPath As String = ConfigurationManager.AppSettings("LogFolder")
    Private mLogPath As String = ""
    ''' <summary>
    ''' 將訊息記錄到檔案中
    ''' </summary>
    ''' <param name="sLogPath">欲記錄Log位置的资料夾名稱 </param>
    ''' <param name="sMsgType">Log類型</param>
    ''' <param name="MsgTxt">欲記錄的訊息</param>
    ''' <remarks></remarks>
    Public Function writeLine(ByVal APName As String, ByVal APUser As String, _
                         ByVal UserIP As String, ByVal sLogPath As String, _
                         ByVal sMsgType As enumMsgType, ByVal MsgTxt As String) As String


        Dim sPrefix As String = ""
        Try
            Select Case sMsgType
                Case enumMsgType.SQLMsg
                    sPrefix = "SQL"
                Case enumMsgType.ErrorMsg
                    sPrefix = "EJ"
                Case enumMsgType.General
                    sPrefix = "LOG"
            End Select
            doWriteLine(mDefaultLogPath.TrimEnd("\") & "\" & sLogPath, sPrefix, MsgTxt, UserIP)
            Return "0"
        Catch ex As Exception
            Return ex.ToString
        End Try


    End Function

    '''<summary>
    ''' 將訊息記錄到檔案中
    ''' </summary>
    ''' <param name="MsgTxt">欲記錄的訊息</param>
    ''' <remarks></remarks>
    Public Function writeLine(ByVal MsgTxt As String) As String
        Try
            doWriteLine("", "", MsgTxt)
            Return "0"
        Catch ex As Exception
            Return ex.ToString
        End Try

    End Function

    ''' <summary>
    ''' 實際執行寫入動作
    ''' </summary>
    ''' <param name="sLogPath">全路徑</param>
    ''' <param name="mPrefix"></param>
    ''' <param name="MsgTxt"></param>
    ''' <remarks></remarks>
    Private Sub doWriteLine(ByVal sLogPath As String, ByVal mPrefix As String, ByVal MsgTxt As String, Optional ByVal ClientIP As String = "")
        Dim Nowdate As String = DateTime.Now.ToString("dd")
        Dim logFileName = ""
        Dim FullLogName As String = ""
        Dim FileStream As FileStream
        Dim oStreamWriter As StreamWriter = Nothing

        ''將要記錄的 Log 作加密處理,另外提供 Log 分析工具 
        'MsgTxt = clsCryptoUtil.Encrypt(MsgTxt)
        Try
            If sLogPath = "" Then
                sLogPath = mDefaultLogPath.Trim("\") & "\"
            Else
                sLogPath = sLogPath.Trim("\") & "\"
            End If

            If System.IO.Directory.Exists(sLogPath) = False Then
                System.IO.Directory.CreateDirectory(sLogPath)
            End If

            If mPrefix = "" Then
                mPrefix = "Log"
            End If

            logFileName = mPrefix & Nowdate & ".htm"
            FullLogName = sLogPath & logFileName

            '寫入同一天
            If MsgTxt.Trim() <> "" Then
                If System.IO.File.Exists(FullLogName) = True Then 'already exist               
                    If Year(FileDateTime(FullLogName)) <> Year(Now) OrElse Month(FileDateTime(FullLogName)) <> Month(Now) Then 'not today's
                        System.IO.File.Delete(FullLogName)
                        FileStream = File.Create(FullLogName)
                        FileStream.Close()
                        oStreamWriter = New StreamWriter(FullLogName, True, System.Text.Encoding.UTF8)
                        oStreamWriter.WriteLine("<table><tr style=""font-family:Arial;font-size:10pt""><td>" & Now.ToString("HH:mm:ss.fff").PadRight(15, " ") & "&nbsp;&nbsp;&nbsp;</td><td>&nbsp;&nbsp;" & ClientIP & "&nbsp;&nbsp;</td><td>" & MsgTxt & "</td></tr></table>")
                        oStreamWriter.Flush()
                    Else
                        '附加文字 
                        oStreamWriter = New StreamWriter(FullLogName, True, System.Text.Encoding.UTF8)
                        oStreamWriter.WriteLine("<table><tr style=""font-family:Arial;font-size:10pt""><td>" & Now.ToString("HH:mm:ss.fff").PadRight(15, " ") & "&nbsp;&nbsp;&nbsp;</td><td>&nbsp;&nbsp;" & ClientIP & "&nbsp;&nbsp;</td><td>" & MsgTxt & "</td></tr></table>")
                        oStreamWriter.Flush()
                    End If
                Else
                    '新增檔案 
                    FileStream = File.Create(FullLogName)
                    FileStream.Close()

                    oStreamWriter = New StreamWriter(FullLogName, True, System.Text.Encoding.UTF8)
                    oStreamWriter.WriteLine("<table><tr style=""font-family:Arial;font-size:10pt""><td>" & Now.ToString("HH:mm:ss.fff").PadRight(15, " ") & "&nbsp;&nbsp;&nbsp;</td><td>&nbsp;&nbsp;" & ClientIP & "&nbsp;&nbsp;</td><td>" & MsgTxt & "</td></tr></table>")
                End If
            End If


        Catch ex As Exception
            Throw New Exception(ex.ToString)

        Finally
            If Not oStreamWriter Is Nothing Then
                oStreamWriter.Flush()
                oStreamWriter.Close()
                oStreamWriter.Dispose()
                oStreamWriter = Nothing
            End If

        End Try
    End Sub

End Class
